package puzzle.slilin2.solver;

import puzzle.common.point.Direction;
import puzzle.common.point.Pnt;
import puzzle.common.solver.topology.TrackAnalyzable;
import puzzle.slilin2.data.SlilinBoard;

class SlilinTrackAnalyzable implements TrackAnalyzable {
	private SlilinBoard m_board;
	private int m_x;
	private int m_y;
	private boolean[] m_visitedFlags;

	public SlilinTrackAnalyzable(SlilinBoard b) {
		m_board = b;
		m_x = 1;
		m_y = 1;
		m_visitedFlags = new boolean[m_board.width * m_board.height];
	}
	
	public Pnt findNextNode() {
		if (m_y > m_board.height - 1) {
			return null;
		}

		Pnt p = new Pnt(m_x, m_y);
		m_x += 2;
		if (m_x > m_board.width - 1) {
			m_x = 1;
			m_y += 2;
		}

		return p;
	}

	public Pnt navigateUnvisitedEdgeFrom(Pnt p0, int trackNumber) {
		for (Direction d : Direction.CLOCKWISE4) {
			int x = p0.x + d.v.dx;
			int y = p0.y + d.v.dy;
			int i = x + y * m_board.width;
			if (!m_visitedFlags[i] && m_board.getLineCellState(x, y).exists()) {
				m_visitedFlags[i] = true;
				return p0.add(d.v, 2);
			}
		}
		return null;
	}
	
}
